; (function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined'
    ? (module.exports = factory(util, Cookies))
    : typeof define === 'function' && define.amd
      ? define(['util', 'Cookies'], factory)
      : // here name is your name
      (global.navInit = factory(util, Cookies))
})(this, function (util, Cookies) {
  return function navInit() {
    $(function () {
      //name-pagecount key-value
      var itemDict = {
        index: 0,
        product: 1,
        channel: 2,
        channelInfo: 1,
        news: 3,
        aboutUs: 4
      }

      //top url
      var url = window.top.location.href
      //get pagecount
      for (var i in itemDict) {
        if (url.indexOf(i) !== -1) {
          var count = itemDict[i]
        }
      }
      //addclass on current dom
      $('ul.commonnav li')
        .eq(count)
        .addClass('active')
      $('#auth2 a.logout').hide()
      if (localStorage.token) {
        $('#auth a.username').text((localStorage.name && localStorage.name !== 'null') ? localStorage.name : localStorage.phone)
        $('img#nav-avatar').attr('src', (localStorage.avatar && localStorage.avatar !== 'null') ? localStorage.avatar : '/assets/images/account.png')
        $('#auth2 a.logout').show()
        if (parseInt(localStorage.userType) === 2) {
          $('#auth a.username').attr('href', '/channel/myprofile.html')
          $('#navaccount a').attr('href', '/channel/myprofile.html')
          $('#channelnav').show()
        } else {
          $('#auth a.username').attr('href', '/my')
          $('#navaccount a').attr('href', '/my')
        }

        $('#auth2 a.logout').on('click', function () {
          localStorage.clear()
          Cookies.remove('Authorization', { path: '/' })
          window.top.location.reload()
        })

        $('#auth').show()
        $('#navaccount').show()
      } else {
        $('#noauth').show()
        $('#navlogin').show()
        $('#noauth a').attr(
          'href',
          '/login.html?redirect_uri=' +
          encodeURIComponent(window.top.location.pathname)
        )
      }
    })
    if (localStorage.token) {
      msgInit()
    }
    function msgInit() {
      util.ajax({
        url:
          '/' +
          (parseInt(localStorage.userType) === 1 ? 'member' : 'manager') +
          '/message/list',
        type: 'POST',
        data: {
          status: 1
        },
        success: function (res) {
          $('a.message-icon span').text(res.data.total || 0)
          $('a.message-icon').attr(
            'href',
            parseInt(localStorage.userType) === 2
              ? '/channel/message.html'
              : '/my#message'
          )

          if (parseInt(res.data.total) !== 0) {
            $('a.message-icon').show()
          }
        }
      })
    }
  }
})
